﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace OrganizationName.Factory.LifeTimeContracts
{
    public class HttpContextLifeTimeContract : KeyedStorageLifeTimeContract
    {
        private System.Web.HttpContext Context
        {
            get
            {
                return HttpContext.Current;
            }
        }

        protected override object GetData(string slotName)
        {
            if (Context == null)
            {
                throw new InvalidOperationException("Http Context is not available");
            }
            return Context.Items[slotName];
        }

        protected override void SetData(string slotName, object instance)
        {
            if (Context == null)
            {
                throw new InvalidOperationException("Http Context is not available");
            }
            Context.Items[slotName] = instance;
        }

        public override object SyncRoot
        {
            get
            {
                return Context != null ? Context : new object();
            }
        }
    }
}
